/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pb.diplom.first.dao;

import com.pb.diplom.first.tables.Commodity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Slava
 */
public class CommodityDao {
    
    private final String login;
    private final String password;
    private final String url;

    public CommodityDao(String login, String password, String url) {
        this.login = login;
        this.password = password;
        this.url = url;
    }

    public List<Commodity> getAll() throws SQLException {
        List<Commodity> goods = new ArrayList<Commodity>();
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, login, password);
            stmt = con.prepareStatement("SELECT * FROM Good");
            rs = stmt.executeQuery();
            while (rs.next()) {
                Commodity commodity = new Commodity();
                commodity.setProducerId(rs.getLong(1));
                commodity.setModel(rs.getString(2));
                commodity.setCategoryId(rs.getInt(3));
                commodity.setPrice(rs.getBigDecimal(4));
                commodity.setDescription(rs.getString(5));
                commodity.setImage(rs.getString(6));
                commodity.setStorage(rs.getString(7));
                goods.add(commodity);
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return goods;
    }
    
}
